package com.kang.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.kang.dao.WarehouseMapper;
import com.kang.pojo.Warehouse;
import com.kang.service.WarehouseService;
import com.vonechina.entity.page.WarehousePage;

/**
 * 仓库服务层实现类
 * 
 * @author zs614
 *
 */
@Service
public class WarehouseServiceImpl implements WarehouseService {
	/**
	 * 仓库持久层
	 */
	@Resource
	private WarehouseMapper whm;

	/**
	 * 分页查询
	 * 
	 * @param warehousePage
	 *            搜索条件实体类
	 * @return
	 */
	@Override
	public List<Warehouse> listWarehouse(WarehousePage warehousePage) {
		warehousePage.setRows(whm.countWarehouse(warehousePage));
		List<Warehouse> list = whm.listWarehouse(warehousePage);
		return list;
	}

	/**
	 * 查询所有仓库
	 * 
	 * @return
	 */
	@Override
	public List<Warehouse> listAllWarehouse() {
		WarehousePage warehousePage = new WarehousePage();
		warehousePage.setPage(false);
		List<Warehouse> list = whm.listWarehouse(warehousePage);
		return list;
	}

	/**
	 * 删除仓库
	 * 
	 * @param warehousemanagementid
	 *            仓库ID
	 * @return
	 */
	@Override
	public int deleteByPrimaryKey(Integer warehousemanagementid) {

		return whm.deleteByPrimaryKey(warehousemanagementid);
	}

	/**
	 * 新增仓库
	 * 
	 * @param record
	 *            仓库实体类
	 * @return
	 */
	@Override
	public int insertSelective(Warehouse record) {

		return whm.insertSelective(record);
	}

	/**
	 * 通过ID搜索仓库
	 * 
	 * @param warehousemanagementid
	 *            仓库ID
	 * @return
	 */
	@Override
	public Warehouse selectByPrimaryKey(Integer warehousemanagementid) {

		return whm.selectByPrimaryKey(warehousemanagementid);
	}

	/**
	 * 更新仓库
	 * 
	 * @param record
	 *            仓库实体类
	 * @return
	 */
	@Override
	public int updateByPrimaryKeySelective(Warehouse record) {

		return whm.updateByPrimaryKeySelective(record);
	}

	/**
	 * 生成仓库编号
	 * 
	 * @return
	 */
	@Override
	public String generatoCode() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String text = sdf.format(new Date()) + "0001";
		Long no = Long.parseLong(text);
		String max = whm.selectMaxCode();
		Long l = 0l;
		if (max != null) {
			l = Long.parseLong(max.replaceAll("[a-zA-Z]", ""));
		}
		if (l >= no) {
			no = l + 1;
		}
		return "CK" + no;
	}
}
